package com.fortune.service.ananlysis;

import com.faneui.common.exception.BusinessException;
import com.fortune.service.ananlysis.dto.BillMailInfoDTO;
import com.fortune.utils.AnanlysisUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service("nbcbCreditCardBillMailAnanlysis")
public class NbcbCreditCardBillMailAnanlysis extends AbstractAnanlysis {

    @Override
    void ananlysis(BillMailInfoDTO billMailInfoDTO,Document document) throws BusinessException {
        Elements es = document.select("td");
        for (Element e : es) {
            String str = e.text();
            if (StringUtils.isNotBlank(str)) {
                str = str.trim().replace(" ", "");
                if (str.contains("尊敬的") && str.contains("先生：")) {
                    String custName = str.substring(str.indexOf("尊敬的") + 3, str.indexOf("先生："));
                    billMailInfoDTO.setCustName(custName);
                } else if (str.contains("请查看您尾号") && str.contains("信用卡")) {
                    String cardCode = str.substring(6, 10);
                    billMailInfoDTO.setCardCode("*" + cardCode);
                    String billMonth = str.substring(13).replace("月账单", "");
                    if (billMonth.length() == 1) {
                        billMonth = "0" + billMonth;
                    }

                    billMailInfoDTO.setBillMonth(billMonth);
                } else if (str.length() == 10) {
                    str = str.replace("-", "");
                    AnanlysisUtils.setDate(billMailInfoDTO, str);
                } else if (str.contains("￥")) {
                    BigDecimal debtMoney = new BigDecimal(str.substring(1));
                    AnanlysisUtils.setDebtMoney(billMailInfoDTO,debtMoney);
                }
            }
        }
    }



}
